package com.huayue.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.huayue.annotation.LogAnno;
import com.huayue.entity.DepotInOrder;
import com.huayue.entity.DepotInOrderDetails;
import com.huayue.entity.User;
import com.huayue.service.DepotInOrderService;
import com.huayue.service.SupplierService;
import com.huayue.util.OrderNumberHelper;
import com.huayue.util.PageHelper;

/**
 * 入库单
 * 
 */
@Controller
@RequestMapping("/depotInOrder")
public class DepotInOrderController {

    @Autowired
    SupplierService ssvc;
    @Autowired
    DepotInOrderService svc;
    BaseController bc = new BaseController();

    /**
     * 准备添加
     * 
     * @param depot
     * @param type
     * @return
     */
    @RequestMapping("/toAdd")
    public ModelAndView toAdd(DepotInOrder depot, String type) {
        // 获取 审核人信息 商品信息 供货商信息
        FastJsonJsonView view = new FastJsonJsonView();
        // 根据前台判断是否需要生成订单号
        if ("true".equals(type)) {
            view.addStaticAttribute("orderNumber", OrderNumberHelper.getOrderNumber(OrderNumberHelper.RKDORDER, "RKD"));
        }

        view.addStaticAttribute("order", svc.findNotInDepotPurchase(depot));
        view.addStaticAttribute("details", svc.findNotInDepotPurchaseDetails(depot));
        return new ModelAndView(view);
    }

    /**
     * 清除订单号
     *
     * @param purchaseorder
     * @return
     */
    @RequestMapping("/close")
    public ModelAndView close(DepotInOrder purchaseorder) {
        // 获取 审核人信息 商品信息 供货商信息
        FastJsonJsonView view = new FastJsonJsonView();
        OrderNumberHelper.removeOrderNumber(OrderNumberHelper.RKDORDER, purchaseorder.getOrdernumber());
        view.addStaticAttribute("code", 0);
        return new ModelAndView(view);
    }

    @RequestMapping("/findYesDetails")
    public ModelAndView findYesDetails(String id) {
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("code", 0);
        DepotInOrder depot = new DepotInOrder();
        depot.setId(Integer.valueOf(id));
        view.addStaticAttribute("order", svc.findOne(depot));
        view.addStaticAttribute("data", svc.findYesInDepotPurchaseDetails(depot));
        return new ModelAndView(view);
    }

    @LogAnno(operateType = "添加-入库单")
    @RequestMapping("/add")
    public ModelAndView add(@RequestBody String json, HttpServletRequest request) {
        // 获取session中该用户信息
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("USER");
        // 格式化json数据
        JSONObject jsonData = JSONObject.parseObject(json);
        // 创建订单信息
        DepotInOrder order = new DepotInOrder();
        order.setEmployee_id_apply(user.getId());
        order.setOrdernumber(String.valueOf(jsonData.get("orderNumber")));
        order.setPurchaseOrderId(Integer.valueOf(String.valueOf(jsonData.get("purchase_order_id"))));
        order.setRemark(jsonData.get("remark").toString());
        JSONArray jsonArray = jsonData.getJSONArray("table");
        // 获取订单详情
        List<DepotInOrderDetails> details = JSONArray.parseArray(jsonArray.toString(), DepotInOrderDetails.class);
        // 将临时订单号销毁
        OrderNumberHelper.removeOrderNumber(OrderNumberHelper.RKDORDER, order.getOrdernumber());
        // 将订单信息和订单详情传至service进行处理
        boolean result = svc.addDetails(order, details);

        // 返回数据给页面
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("code", 0);
        if (result) {
            view.addStaticAttribute("icon", 1);
            view.addStaticAttribute("msg", "订单添加成功！");
        } else {
            view.addStaticAttribute("icon", 2);
            view.addStaticAttribute("msg", "订单添加失败！");
        }
        return new ModelAndView(view);
    }

    @LogAnno(operateType = "修改-入库单")
    @RequestMapping("/update")
    public ModelAndView update(DepotInOrder depotInOrder) {
        return bc.update(svc, depotInOrder);
    }

    @LogAnno(operateType = "删除-入库单")
    @RequestMapping("/delete")
    public ModelAndView delete(DepotInOrder depotInOrder) {
        return bc.delete(svc, depotInOrder);
    }

    @RequestMapping("/findOne")
    public ModelAndView findOne(DepotInOrder depotInOrder) {
        return bc.findOne(svc, depotInOrder);
    }

    @RequestMapping("/findPage")
    public ModelAndView findPage(PageHelper page, DepotInOrder depotInOrder) {
        return bc.findPage(svc, page, depotInOrder, depotInOrder);
    }

    @RequestMapping("/findPageYes")
    public ModelAndView findPageYes(PageHelper page, DepotInOrder depotInOrder) {
        FastJsonJsonView view = new FastJsonJsonView();
        PageHelper pages = new PageHelper();
        pages.setCount(svc.countYes(depotInOrder));
        pages.setPageNow(page.getPageNow() == 0 ? 1 : Integer.valueOf(page.getPageNow()));
        pages.setPageSize(page.getPageSize() == 0 ? 5 : Integer.valueOf(page.getPageSize()));
        view.addStaticAttribute("code", 0);
        view.addStaticAttribute("message", "查询成功");
        view.addStaticAttribute("count", pages.getCount());
        view.addStaticAttribute("data", svc.findPageYes(pages, depotInOrder));
        return new ModelAndView(view);
    }
}
